﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace YFrameWork.RunTime
{
   
    public class AudioComponent :YFrameWorkComponent 
    {
        
        private AudioSource mBgAudioSource;
        private AudioSource mDefaultAudioSource;
        private float NormalAudioVolume = 0.7f;
        public readonly Dictionary<string, AudioClip> AllAudioClip = new Dictionary<string, AudioClip>();
        public override void ToCreate()
        {
          
        }
        public void SetInit(GameObject mainCamera)
        {
            mBgAudioSource = CreateAudioSource("Audio_BG", mainCamera);
            mDefaultAudioSource= CreateAudioSource("Audio_Default", mainCamera);
        }

        private AudioSource CreateAudioSource(string audioName,GameObject mainCamera)
        {
            GameObject audio = new GameObject(audioName);
            audio.transform.SetParent(mainCamera.transform);
            return audio.GetOrAddComponent<AudioSource>();
        }
       
        #region 背景音乐
        /// <summary>
        /// 播放背景音乐（string = 要播放的音乐名字）
        /// </summary>
        public void PlaySoundBg(string soundName,float volume = 0.5f,bool isLoop = true)
        {
            PlaySound(mBgAudioSource, AddOrGetAudioClip(soundName), volume, true);
        }
        /// <summary>
        /// 停止播放背景音乐
        /// </summary>
        public void BGStopMusic()
        {
            StopSound(mBgAudioSource);
        }
        /// <summary>
        /// 暂停背景音乐
        /// </summary>
        public void BGPauseMusic()
        {
            PauseSound(mBgAudioSource);
        }
        /// <summary>
        /// 恢复背景音乐的播放
        /// </summary>
        public void BGResumeMusic(string audioName)
        {
            ResumeSound(mBgAudioSource);
            if (mBgAudioSource.clip == null)
            {
                PlaySoundBg(audioName);
            }
        }

        #endregion
        #region 正常音乐的播放
        /// <summary>
        /// 正常音乐静音
        /// </summary>
        public void MuteNormal()
        {
            StopSound(mDefaultAudioSource);
            NormalAudioVolume = 0;
            mDefaultAudioSource.volume = NormalAudioVolume;
        }
        /// <summary>
        /// 正常音乐恢复静音
        /// </summary>
        public void NoMuteNormal(float _Volume = 0.7f)
        {
            NormalAudioVolume = _Volume;
            mDefaultAudioSource.volume = NormalAudioVolume;
        }

        /// <summary>
        /// 正常音乐的播放（string = 要播放的音乐名字）
        /// </summary>
        public void PlaySoundNormal(string soundName)
        {
            if (NormalAudioVolume <= 0) return;
            PlaySound(mDefaultAudioSource, AddOrGetAudioClip(soundName), NormalAudioVolume);
        }
        #endregion
        #region  封装功能
        /// <summary>
        /// 停止音乐的播放（audioSource = 要停止的音乐播放器）
        /// </summary>
        private void StopSound(AudioSource audioSource)
        {
            audioSource.Stop();
        }
        /// <summary>
        /// 暂停的播放（audioSource = 要停止的音乐播放器）
        /// </summary>
        private void PauseSound(AudioSource audioSource)
        {
            audioSource.Pause();
            audioSource.mute = true;
        }
        /// <summary>
        /// 继续音乐的播放（audioSource = 要停止的音乐播放器）
        /// </summary>
        private void ResumeSound(AudioSource audioSource)
        {
            audioSource.UnPause();
            audioSource.mute = false;
        }
        /// <summary>
        /// 播放一个音乐（audioSource = 选择音乐源,audioClip = 要播放的音乐,Volume = 音量大小，loop = 是否循环，默认为false不循环）
        /// </summary>
        private void PlaySound(AudioSource audioSource, AudioClip audioClip, float Volume, bool loop = false)
        {
            audioSource.clip = audioClip;
            audioSource.loop = audioClip;
            audioSource.volume = Volume;
            audioSource.loop = loop;
            audioSource.mute = false;
            audioSource.Play();
        }

        #endregion



        /// <summary>
        /// 获取或者添加音乐片段
        /// </summary>
        /// <param name="audioName"></param>
        /// <returns></returns>
        public AudioClip AddOrGetAudioClip(string audioName)
        {
            if (!AllAudioClip.ContainsKey(audioName))
            {
                AudioClip clip = LoadSound(audioName);
                if (clip != null) 
                    AllAudioClip.Add(audioName, clip);
                return clip;
            }
            else
            {
                return AllAudioClip[audioName];
            }
        }
        /// <summary>
        /// 在resources内加载一个音乐（string = 音乐的名字）
        /// </summary>
        private AudioClip LoadSound(string sourceAssetsName)
        {
            AudioClip clip = GameEntry.Instance.GetComponent<ResourceComponent>().LoadResource<AudioClip>(sourceAssetsName);
            if (clip == null)
            {
                Log.Error("要加载的音乐没有在资源中找到：{0}", sourceAssetsName);
            }
            return clip;

        }
    }
}